const Stack = require('./stack')
const templs = [73, 74, 75, 71, 69, 72, 76, 73]

// => [1, 1, 4, 2, 1, 1, 0, 0]

function change(templs) {
  // 创建一个栈
  const stack = new Stack();
  const len = templs.length;
  const arr = []; // 存放结果的数组
  // 遍历templs
  for (let i = 0; i < len; i++) {
    let j = i;
    while (j < len && templs[j] <= templs[i]) {
      stack.push(templs[j]);
      j++;
    }
    if (j === len) {
      arr.push(0)
    } else {
      arr.push(stack.len())
    }
    // 清空栈
    stack.clear();
  }
  return arr;
}
console.log(change(templs));